package com.cnt.controller;

import com.cnt.dto.AddPointsRuleDTO;
import com.cnt.entity.Points;
import com.cnt.entity.PointsRules;
import com.cnt.service.PointsService;
import com.cnt.vo.ResultVo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.MediaType;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import java.io.BufferedInputStream;
import java.util.List;

@RestController
@RequestMapping("/api/points")
public class PointsController {


    @Autowired
    private PointsService pointsService;

    @GetMapping("/list")
    public ResultVo<List<Points>> getPointsList(){
        return pointsService.getPointsList();
    }

    @GetMapping("/getPointsInfo")
    public ResultVo<List<PointsRules>> getPointsInfo(@RequestParam("pointsId") Integer pointsId){
        return pointsService.getPointsInfo(pointsId);
    }

    @PostMapping("/create")
    public ResultVo<Void> createPoints(@RequestBody Points points){
        System.out.println(points);
        return pointsService.createPoints(points);
    }

    @PostMapping("/changePoints")
    public ResultVo<Void> changePoints(@RequestBody Points points){
        return pointsService.changePoints(points);
    }

    @PostMapping("/deletePoints")
    public ResultVo<Void> deletePoints(@RequestBody Points points){
        return pointsService.deletePoints(points.getPointsId());
    }

    @PostMapping("/addPointsRule")
    public ResultVo<Void> addPointsRule(@RequestBody AddPointsRuleDTO addPointsRuleDTO){

        return pointsService.addPointsRule(addPointsRuleDTO.getPointsId(), addPointsRuleDTO.getCount());
    }

    @PostMapping("/uploadPointsRule")
    public ResultVo<Void> uploadPointsRule(@RequestBody PointsRules pointsRules){
        return pointsService.uploadPointsRule(pointsRules);
    }

    @PostMapping("/deletePointsRule")
    public ResultVo<Void> deletePointsRule(@RequestBody PointsRules pointsRules){
        return pointsService.deletePointsRule(pointsRules.getPointsRulesId());
    }

    @PostMapping("/replacePoints")
    public ResultVo<Void> replacePoints(@RequestBody PointsRules pointsRules){
        return pointsService.replacePoints(pointsRules.getPointsId());
    }

    @PostMapping("/copy")
    public ResultVo<Void> copyPoints(@RequestBody PointsRules pointsRules){
        return pointsService.copyPoints(pointsRules.getPointsId());
    }

    @PostMapping("/uploadFile")
    public ResultVo<Void> uploadFile(@RequestParam("file") MultipartFile file,
                                     @RequestParam(value="pointsId", required=false) Integer pointsId){

        return pointsService.uploadFile(file,pointsId);
    }

    @GetMapping(value = "/download", produces = MediaType.APPLICATION_OCTET_STREAM_VALUE)
    public ResponseEntity<byte[]> download(@RequestParam(value="pointsId", required=false) Integer pointsId) {

        try (BufferedInputStream bis = new BufferedInputStream(pointsService.download(pointsId), 4096)) {
            byte[] data = new byte[bis.available()];
            bis.read(data);
            if (data.length == 0) {
                return ResponseEntity.status(500).body(null);
            }
            return ResponseEntity.ok()
                    .header("Content-Disposition", "attachment; filename=points.doc")
                    .body(data);
        } catch (Exception e) {
            return ResponseEntity.status(500).body(null);
        }
    }
}
